﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NPMonitor.Implementation;
using NPMonitor.DataModel;
using NPMonitor.Web.Code;

namespace NPMonitor.Web.UserControls
{
    public partial class JobList : System.Web.UI.UserControl
    {
        #region Properties

        protected int PageSize
        {
            get { return ctPager.PageSize; }
        }

        #endregion

        #region Methods

        private void BindData(int pageIndex)
        {
            int jobTotalCount = StoreManager.Current.JobDefinitionStore.GetJobDefinitionTotalCount();

            ctPager.IntialProperties(jobTotalCount);
            lblLRTime.InnerText = DateTime.Now.ToFormatString();

            if (pageIndex > ctPager.TotalPage - 1)
            {
                pageIndex = ctPager.TotalPage - 1;
            }

            List<JobDefinition> jobList = StoreManager.Current.JobDefinitionStore.GetAllJobDefinitions(pageIndex, PageSize);

            lblJobsCount.InnerText = jobTotalCount.ToString();
            rptJobList.DataSource = jobList;
            rptJobList.DataBind();
        }

        #endregion

        public JobList()
        {
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ctPager.PageChange += new PagerControl.PageRefresh(BindData);
            ctJobInfo.OnSave += new JobInfo.OnSaveHandler(ctJobInfo_OnSave);

            if (!IsPostBack)
            {
                BindData(0);
            }
        }

        void ctJobInfo_OnSave(object sender, JobSaveEventArgs e)
        {
            if (e.Operation == "Create")
            {
                BindData(0);
            }
            else
            {
                BindData(ctPager.CurrentPageIndex);
            }
        }

        protected void lbtnRefresh_OnClick(object sender, EventArgs e)
        {
            BindData(ctPager.CurrentPageIndex);
        }

        protected void rptJobList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                if (e.CommandName == "Edit")
                {
                    int id = 0;

                    if (int.TryParse(e.CommandArgument.ToString(), out id))
                    {
                        ctJobInfo.JobId = id;

                        ctJobInfo.ShowJobInfo(id, "Edit");
                    }
                }
                else if (e.CommandName == "View")
                {
                    int id = 0;

                    if (int.TryParse(e.CommandArgument.ToString(), out id))
                    {
                        ctJobInfo.JobId = id;
                        ctJobInfo.ShowJobInfo(id, "View");
                    }
                }
                else if (e.CommandName == "Delete" && e.CommandArgument != null)
                {
                    int id = 0;

                    if (int.TryParse(e.CommandArgument.ToString(), out id))
                    {
                        StoreManager.Current.JobDefinitionStore.DeleteJobDefinitionById(id);
                    }

                    BindData(ctPager.CurrentPageIndex);
                }
            }
        }

        protected void lbtnCreateJob_Click(object sender, EventArgs e)
        {
            ctJobInfo.Reset();
            ctJobInfo.ShowJobInfo(viewMode: "Create");
        }

        protected void rptJobList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            JobDefinition data = e.Item.DataItem as JobDefinition;
            if (data != null)
            {
                LinkButton lbtnDeleteJob = e.Item.FindControl("lbtnDeleteJob") as LinkButton;
                lbtnDeleteJob.OnClientClick = string.Format("return JobDeleteVerification({0});", data.JobId);
            }
        }
    }
}